package com.zx.idc.backend.gui.api.watch.handler.base;

import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.watch.handler.DefaultResultHandler;
import com.zx.idc.common.spring.SpringBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * FTP结果处理服务提供者
 */
@Component
public class FtpUploadResultHandelProvider {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    private List<AbstractFtpUploadResultHandler> handlers;

    @Autowired
    private DefaultResultHandler defaultResultHandle;

    public FtpUploadResultHandelProvider() {
        handlers = SpringBeanUtil.getBeansOfType(AbstractFtpUploadResultHandler.class)
            .entrySet()
            .stream()
            .map(set -> set.getValue())
            .collect(Collectors.toList());
    }

    /**
     * 执行操作
     * @param stat
     */
    public void execute(Stat stat) {

        String type = stat.getName().substring(0, 1);
        handlers.stream()
            .filter(handler -> handler.adapter(type))
            .findFirst()
            .orElse(defaultResultHandle)
            .handleTemplate(stat);

    }

}
